import express from 'express';
import { statisticsController } from '../controllers/statisticsController';
import { roleAuth } from '../middleware/roleAuth';
import { Role } from '@prisma/client';
import { validateRequest } from '../middleware/validation';
import { z } from 'zod';

const router = express.Router();

// 参数验证schema
const schoolIdSchema = z.object({
  schoolId: z.string().uuid('无效的学校ID')
});

const filterSchema = z.object({
  classIds: z.string().optional().transform(val => val?.split(',')),
  grades: z.string().optional().transform(val => val?.split(',').map(Number))
});

// 路由配置
router.get(
  '/:schoolId/school-statistics',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(schoolIdSchema),
  statisticsController.getSchoolStatistics
);

router.get(
  '/:schoolId/class-comparison',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(schoolIdSchema.merge(filterSchema)),
  statisticsController.getClassComparison
);

router.get(
  '/:schoolId/grade-comparison',
  roleAuth([Role.ADMIN, Role.TEACHER]),
  validateRequest(schoolIdSchema.merge(filterSchema)),
  statisticsController.getGradeComparison
);

export default router;
